package com.example.ycdyus.dailyfruit.fragment;


import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.GuoyouHeadAdapter;
import com.example.ycdyus.dailyfruit.adapter.GuoyuquanListViewAdapter;
import com.example.ycdyus.dailyfruit.bean.GuoyouquanBean;
import com.example.ycdyus.dailyfruit.bean.Main;
import com.example.ycdyus.dailyfruit.bean.Top;
import com.example.ycdyus.dailyfruit.constant.URLConstant;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_guoyouquan)
public class GuoyouquanFragment extends Fragment {


    @ViewById
    PullToRefreshListView mainGuoyouPulltorefresh;
    private List<Main>  mainList =  new ArrayList<>();
    private GuoyuquanListViewAdapter  guoyuquanListViewAdapter;
    private GuoyouHeadAdapter headAdapter;
    private List<Top> topList = new ArrayList<>();
    private ListView listView;
    private int page = 1;
    private HttpUtils httpUtils;

    @AfterViews
    public void initData(){
        httpUtils = new HttpUtils();
        guoyuquanListViewAdapter = new GuoyuquanListViewAdapter(getActivity(),mainList,R.layout.item_guoshi_guoyou_listview);
        mainGuoyouPulltorefresh.setAdapter(guoyuquanListViewAdapter);

        listView = new ListView(getActivity());
        listView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 360));
        headAdapter= new GuoyouHeadAdapter(getActivity(),topList,R.layout.item_guoyou_head_ll);
        listView.setAdapter(headAdapter);

        mainGuoyouPulltorefresh.getRefreshableView().addHeaderView(listView);
        mainGuoyouPulltorefresh.setMode(PullToRefreshBase.Mode.BOTH);
        mainGuoyouPulltorefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mainGuoyouPulltorefresh.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                downData();
            }
        });

        downData();
    }
    //加载数据
    private void downData() {
        httpUtils.send(HttpRequest.HttpMethod.POST, URLConstant.MAIN_GUOSHI_GUOYOUQUAN_POST, getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                mainGuoyouPulltorefresh.onRefreshComplete();
                GuoyouquanBean bean = JSONObject.parseObject(responseInfo.result,GuoyouquanBean.class);
                if(bean!=null&&bean.getMain()!=null){
                    mainList.clear();
                    topList.clear();

                    mainList.addAll(bean.getMain());
                    guoyuquanListViewAdapter.notifyDataSetChanged();

                    topList.addAll(bean.getTop());
                    headAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
    //参数
    private RequestParams getRequestParams(){
        RequestParams params =  new RequestParams();
        params.addBodyParameter("sign","a58f202c25bb071bf80fcc1708c27319");
        params.addBodyParameter("limit","20");
        params.addBodyParameter("timestamp","1445431785");
        params.addBodyParameter("platform","ANDROID");
        params.addBodyParameter("source","app");
        params.addBodyParameter("page",String.valueOf(page));
        params.addBodyParameter("connect_id","161c2ae532723d329ee711d284a1b36e");
        params.addBodyParameter("region_id","106092");
        params.addBodyParameter("service","fruit.getMaxArticleList");
        params.addBodyParameter("device_id","1b8ba62bf241cee5b8c317d07c0c503e");
        params.addBodyParameter("channel","portal");
        params.addBodyParameter("version","3.0.3");
        return params;
    }
}
